<?php
/**
 *author:韩帅
 *createtime:2017/6/21 14:17
 *description:
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class UserServerBalance extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wl_user_server_balance_log';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 定义银行卡提现类型
     */
    const TYPE_WIDTHDRAW = 0;

    /**
     * 定义收入类型
     */
    const ACTION_TYPE_IN = 2;

    /**
     * 定义支出类型
     */
    const ACTION_TYPE_GO = 1;

    /**
     * 定义状态
     */
    const STATUS_ONE    = 0;

    /**
     * 定义状态
     */
    const STATUS_TWO    = 1;

    /**
     * 定义状态
     */
    const STATUS_THREE  = 2;

    /**
     * 定义状态
     */
    const STATUS_FOUR   = 3;

    /**
     * @var array 订单类型
     */
    public $type       = ['0' => '银行卡提现','1' => '退货','2' => '订单返利','3' => '购买消费'];

    /**
     * 收入支出类型
     */
    private $action_type = ['0' => '其他','1' => '支出','2' => '收入'];

    /**
     * 订单状态
     */
    private $status     = ['0' => '待审核','1' => '审核中','2' => '审核通过','3' => '审核失败'];

    /**
     * 处理交易明细列表
     */
    public  function translate($data)
    {
        $newArr                 = [];


        foreach ($data['data'] as $key => $val) {

            empty($val['order_sn']) && $val['order_sn'] = '';

            empty($val['note'])     && $val['note']     = '';



            if (!empty($val['money']) ) {

                $val['action_type'] === self::ACTION_TYPE_GO && $val['money'] = '-'.number_format($val['money'],2);
                $val['action_type'] === self::ACTION_TYPE_IN && $val['money'] = '+'.number_format($val['money'],2);
            } else {

                $val['money'] = 0.00;
            }

            $month = date('m',strtotime($val['created_at']));
            $arr['month']       = $month;
            $val['created_at']  = strtotime($val['created_at']); //这里的时间转化为时间戳,要写到month后面
            $arr['data'][]      = $val;


            $newArr[$month]['month']  = $month;
            $newArr[$month]['list'][] = $val;





/*            $newArr['balance_log'][] = ['order_sn'    => $v['order_sn'] ,  'money'   => $v['money'],'type'        => $v['type'],
                'action_type' => $v['action_type'],'status'  => $v['status'],'created_at' => $v['created_at'],
                'icon'       => ''               ,'month'   => $month,
            ];*/


//            if (!empty($v['type'])) {
//
//                $v['type'] = $this->type[$v['type']];
//            }
//
//            if (!empty($v['action_type'])) {
//
//                $v['action_type'] = $this->action_type[$v['action_type']];
//            }

//            $v['status'] = $this->status[intval($v['status'])];

/*            if (empty($v['note'])) {

                $v['note'] = '';
            }
            $month = date('m',strtotime($v['created_at']));

            $newArr['balance_log'][] = ['order_sn'    => $v['order_sn'] ,  'money'   => $v['money'],'type'        => $v['type'],
                                        'action_type' => $v['action_type'],'status'  => $v['status'],'created_at' => $v['created_at'],
                                         'icon'       => ''               ,'month'   => $month,
                                                ];*/
        }
        //$arr['balance_log'][]  = $newArr;


        $arr = [];
        foreach ($newArr as $key => $val) {
            $arr[] = $val;
        }

        $arrData['per_page']     = $data['per_page'];
        $arrData['current_page'] = $data['current_page'];
        $arrData['last_page']    = $data['last_page'];
        $arrData['balance_log']  = $arr;
        return $arrData;
    }

    /**
     * 获取银行卡号和类型
     */
    public static function getCard($id) {

        $info = [];
        $info = DB::table('wd_user_card')->join('wd_card_type','wd_user_card.card_type','=','wd_card_type.id')
                                         ->where('wd_user_card.id',$id)
                                         ->select('wd_user_card.card_num','wd_card_type.name')
                                         ->first();
        return $info;
    }

    /**
     * 订单支付成功,新增一条记录
     */
    public static function insertLog($user_id,$order_sn,$money) {

        $params = [];

        $user_id  == null || $params['user_id']  = $user_id;
        $order_sn == null || $params['order_sn'] = $order_sn;
        $money    == null || $params['money']    = $money;

        if (count($params) <3 ) {

            return false;
        }
        $params['created_at']  = date('Y-m-d H:i:s', time());
        $params['updated_at']  = date('Y-m-d H:i:s', time());
        $params['type']        = 2;
        $params['action_type'] = 1;

        $res = DB::table('wl_user_server_balance_log')->insert($params);

        if ($res == true) {

            return true;
        }
        return false;
    }
}
